// 线性探查
// 当向表中某个位置加入一个新元素的时候，
// 如果索引为index的位置已经被占据，就尝试index+1的位置。
// 如果index+1的位置也被占据了，就尝试index+2的位置，以此类推

function HashTable(){
	var table = [];

	var ValuePair = function(key, value){
		this.key = key;
		this.value = value;
	}

	var loseloseHashCode = function(key){
		var hash = 0;

		for(var i = 0; i < key.length; i++){
			hash += key.charCodeAt(i);
		}
		return hash % 37;
	}

	this.put = function(key, value){
		var position = loseloseHashCode(key);

		if(table[position] == undefined){
			table[position] = new ValuePair(key, value);
		} else {
			var index = ++position;
			while(table[index] != undefined){
				index++;
			}
			table[index] = new ValuePair(key, value);
		}
	}

	this.get = function(key){
		var position = loseloseHashCode(key);

		if(table[position] != undefined){
			if(table[position].key === key){
				return table[position].value;
			} else {
				var index = ++position;
				while(table[index] === undefined || table[index].key !== key){
					// table[index] === undefined 可能是由于remove方法造成的
					index++;
				}
				if(table[index].key === key){
					return table[index].value;
				}
			}
		}
		return undefined
	}

	this.remove = function(key){
		var position = loseloseHashCode(key);

		if(table[position] != undefined){
			if(table[position].key === key){
				table[position] = undefined;
				return true;
			}
			var index = ++position;
			while(table[index] === undefined || table[index].key !== key){
				index++;
			}
			if(table[index].key === key){
				table[index] = undefined;
				return true;
			}
		}
		return false;
	}
}